package com.yk.spark.dao.app.impl;

import com.yk.spark.dao.app.api.IAppDAO;
import com.yk.spark.dao.common.api.ICommonDAO;
import com.yk.spark.model.AppModel;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import java.util.List;

/**
 * Created by Limit on 2017/8/23.
 */
@Repository("appDAO")
public class AppDAOImpl implements IAppDAO{

    @Resource(name = "commonDAO")
    private ICommonDAO commonDAO;
    /**
     * 查询所有app
     *
     * @return 所有app的列表
     */
    @Override
    public List<AppModel> queryAll() {
        String hql="from AppModel as model order by model.updateTime desc ";
        Query<AppModel> query=this.commonDAO.getSession().createQuery(hql,AppModel.class);
        return query.list();
    }

    /**
     * 分页查询APP
     *
     * @param start 分页起始位置
     * @param limit 最大查询结果数
     * @return 本次分页APP的列表
     */
    @Override
    public List<AppModel> queryPage(int start, int limit) {
        String hql="from AppModel as model order by model.updateTime desc ";
        Query<AppModel> query=this.commonDAO.getSession().createQuery(hql,AppModel.class);
        query.setFirstResult(start);
        query.setMaxResults(limit);
        return query.list();
    }

    /**
     * 根据AppID获取App
     *
     * @param appId AppModel唯一标识
     * @return AppModel实体
     */
    @Override
    public AppModel queryByAppId(String appId) {
        String hql="from AppModel as model where model.appId = :appId";
        Query<AppModel> query=this.commonDAO.getSession().createQuery(hql,AppModel.class);
        query.setParameter("appId",appId);
        return query.getSingleResult();
    }

    /**
     * 查询app总数
     *
     * @return app总数
     */
    @Override
    public long querySum() {
        String hql="select count(*) from AppModel";
        Query<Long> query=this.commonDAO.getSession().createQuery(hql,Long.class);
        return query.getSingleResult();
    }
}
